<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #red{
            color: red;
        }
    </style>
</head>

<body>
    <div id="app" class="score-case">
        <div class="table">
            <table>
                <thead>
                    <tr>
                        <td>编号</td>
                        <td>科目</td>
                        <td>成绩</td>
                        <td>操作</td>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item,index) in list" :key="item.id">
                        <td>{{index +1}}</td>
                        <td>{{item.subject}}</td>
                        <td :class="{red: item.score < 60}">{{item.score}}</td> 
                        <td><a @click.prevent="del(item.id)" href="#">删除</a></td>
                    </tr>
                    请输入科目:<input type="text" v-model.trim="subject"><br>
                    请输入分数:<input type="text" v-model.number="score"><br>
                    <button @click="add" class="submit">添加</button>
                </tbody>
                <div id="red"></div>

            </table>
            <tfoot>
                <span>总分:{{totalscore}}</span>
                <span>平均分：{{averscore}}</span>
            </tfoot>
        </div>
    </div>
    <script src="./vue.js"></script>
    <script>
        
        
        const app = new Vue({
            el: "#app",
            data: {
                list: [
                    { id: 1, subject: "语文", score: 20 },
                    { id: 2, subject: "数学", score: 100 },
                    { id: 3, subject: "英语", score: 70 },
                ],
                subject: "",
                score: ""
            },
            computed: {
                totalscore() {
                    return this.list.reduce((sum, item) => sum + item.score, 0)
                },
                averscore() {
                    if(this.list.length==0){
                        return 0
                    }
                    return (this.totalscore/this.list.length).toFixed(2)
                }


            },
            methods: {
                del(id) {
                    console.log(id);
                    return this.list = this.list.filter(item => item.id != id)
                },
                add() {
                    if (!this.subject) {
                        alert("笨蛋")
                        return
                    }

                    if (typeof (this.score) !== "number") {
                        alert("傻吊")
                        return
                    }
                    this.list.push({
                        id: +new Date(),
                        subject: this.subject,
                        score: this.score
                    })
                    this.subject = "",
                        this.score = ""
                    
                }
            }


        })

    </script>
</body>

</html>